package wtws.message;

import java.nio.ByteBuffer;
import java.util.ArrayList;

import wtws.message.detail.AlphanumericProductSubBlock;
import wtws.message.interf.TdwrProdMessage;
import wtws.util.ByteUtil;

public class AlphanumericProduct implements TdwrProdMessage {

	private static final long serialVersionUID = 1L;
	public static final int MSG_ID = 9233;
	public static final String TITLE = "DFU Alphanumeric Product";
	public static final int MSG_LEN = 8;

	private int messageId;
	private int messageLength;
	private int messageClass;
	private int numberOfANMsg;
	private ArrayList<AlphanumericProductSubBlock> subBlock;

	public AlphanumericProduct(int messageClass, ArrayList<AlphanumericProductSubBlock> subBlock) {
		this.messageId = MSG_ID;
		this.messageClass = messageClass;
		this.numberOfANMsg = subBlock.size();
		this.subBlock = subBlock;
		this.messageLength = MSG_LEN;
		for (AlphanumericProductSubBlock sb : subBlock) {
			this.messageLength += sb.toByteArray().length;
		}
	}

	@Override
	public byte[] toByteArray() {
		ByteBuffer br = ByteBuffer.allocate(messageLength);
		br.put(ByteUtil.to2Bytes(messageId));
		br.put(ByteUtil.to2Bytes(messageLength));
		br.put(ByteUtil.toByte(messageClass));
		br.put(ByteUtil.toByte(numberOfANMsg));
		br.put(ByteUtil.EMPTY_2B);
		for (AlphanumericProductSubBlock sb : subBlock) {
			br.put(sb.toByteArray());
		}
		return br.array();
	}

	@Override
	public String getMessageTitle() {
		return TITLE;
	}

	@Override
	public String toString() {
		StringBuffer buf = new StringBuffer();
		buf.append(messageId);
		buf.append(",");
		buf.append(messageLength);
		buf.append(",");
		buf.append(numberOfANMsg);
		buf.append("\nSubBlock::\n");
		for (AlphanumericProductSubBlock sb : subBlock) {
			buf.append(sb.toString());
			buf.append("\n");
		}
		return buf.toString();
	}

	public int getMessageId() {
		return messageId;
	}

	public void setMessageId(int messageId) {
		this.messageId = messageId;
	}

	public int getMessageLength() {
		return messageLength;
	}

	public void setMessageLength(int messageLength) {
		this.messageLength = messageLength;
	}

	public int getMessageClass() {
		return messageClass;
	}

	public void setMessageClass(int messageClass) {
		this.messageClass = messageClass;
	}

	public int getNumberOfANMsg() {
		return numberOfANMsg;
	}

	public void setNumberOfANMsg(int numberOfANMsg) {
		this.numberOfANMsg = numberOfANMsg;
	}

	public ArrayList<AlphanumericProductSubBlock> getSubBlock() {
		return subBlock;
	}

	public void setSubBlock(ArrayList<AlphanumericProductSubBlock> subBlock) {
		this.subBlock = subBlock;
	}

	@Override
	public int getId() {
		return MSG_ID;
	}

}
